package com.etoak.controller;

import com.etoak.common.core.vo.ResultVO;
import com.etoak.entity.Cart;
import com.etoak.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/cart")
public class CartController {
    @Autowired
    CartService cartService;

    // 添加修改购物车
    @PostMapping
    public ResultVO<Object> saveOrUpdate(@Validated @RequestBody Cart cart) {
        cartService.saveOrUpdate(cart);
        return ResultVO.success();
    }

    // 获取购物车列表 get/cart/list
    @GetMapping("/list")
    public ResultVO<List<Cart>> list(){
        List<Cart> list = cartService.getList();
        return ResultVO.success(list);
    }

    // 删除某一个商品
    @DeleteMapping("/dishes/{id}")
    public ResultVO<Object> deleteDishes(@PathVariable String id) {
        cartService.delete(id);
        return ResultVO.success();
    }
    @DeleteMapping
    public ResultVO<Object> clear(){
        cartService.clear();
        return ResultVO.success();
    }

}
